<?php
namespace backend\service ;
use backend\models\Column;
use Yii ;
use yii\db\ActiveQuery;
use yii\helpers\Url;

//公共服务
class CommonService {

    /*
     * 检查重复提交
     */
    public static function checkRepeatSubmit($event){
        $controller= Yii::$app->controller->id;
        $action = Yii::$app->controller->action->id;

        //处理post请求
        if(Yii::$app->request->isPost==false){
            return ;
        }

        //index一般都是列表,不做检查
        if($action=="index" || $action=="upload-mul-image" || $action=="error" || $action=="store-info"){
            return ;
        }

        $sessionId = Yii::$app->session->id ;
        $route = Url::to();
        $cache = Yii::$app->cache ;
        $repeatCheckKey = $sessionId.$route ;
        if($cache->get($repeatCheckKey) !== false ){
            Yii::$app->controller->redirect(["tip/index","msg"=>"提交太快,请稍后再试", "url" => '']);
            Yii::$app->end() ;
        }
        $cache->set($repeatCheckKey ,time(),2) ;
    }

    /**
     * 获取当前位置
     * @return mixed|string
     */
    public static function getLocation()
    {
        $title = '';
        $controller= Yii::$app->controller->id;
        $action = Yii::$app->controller->action->id;
        $column = Column::find()->where(['tag' => $controller])->one();
        if ($column) {
            $title = $column->name;
            $subColumn = Column::find()->where(['tag' => $action, 'pid' => $column->id])->one();
            if ($subColumn) {
                $title .= ' > ' . $subColumn->name;
            }
        }

        return $title;
    }

    /**
     * 自动过滤搜索字段并组相关的筛选条件语句(目前支持"=/LIKE/between/callback"
     * @param $model ActiveQuery
     * @param $maps array
     */
    public static function filterSearchMaps(&$model, $maps)
    {
        $getData = Yii::$app->request->get();
        foreach ($maps as $map) {
            if (is_string($map)) {
                if (isset($getData[$map]) && $getData[$map] !== '-1' && $getData[$map] !== '') {
                    $model->andWhere([$map => $getData[$map]]);
                }
            } elseif (strtolower($map[0]) === 'like') {
                if (isset($getData[$map[1]]) && $getData[$map[1]] !== '-1' && $getData[$map[1]] !== '') {
                    $model->andWhere(['LIKE', $map[1], $getData[$map[1]]]);
                }
            } elseif (strtolower($map[0]) === 'between') {
                if (isset($getData[$map[1] . '_begin']) && $getData[$map[1] . '_begin'] !== '-1' && $getData[$map[1] . '_begin'] !== '') {
                    $model->andWhere(['>=', $map[1], $getData[$map[1] . '_begin']]);
                }
                if (isset($getData[$map[1] . '_end']) && $getData[$map[1] . '_end'] !== '-1' && $getData[$map[1] . '_end'] !== '') {
                    $model->andWhere(['<=', $map[1], $getData[$map[1] . '_end']]);
                }
            } elseif (strtolower($map[0]) === 'callback') {
                if (isset($getData[$map[1]]) && $getData[$map[1]] !== '-1' && $getData[$map[1]] !== '' && $map[2] instanceof \Closure ) {
                    $map[2]($model, $getData[$map[1]]);
                }
            }
        }
    }

    /**
     * 电话号码过滤
     * @param $phone
     * @return mixed
     */
    public static function phoneFilter($phone)
    {
        return substr_replace($phone,'****',3,4);
    }

    /**
     * 其他账号过滤
     * @param $account
     * @return mixed
     */
    public static function otherFilter($account)
    {
        $filterLen = mb_strlen($account);
        $filterLen = $filterLen < 4 ? $filterLen : 4;
        $replaceStr = '';
        for ($i = 0; $i < $filterLen; $i++) {
            $replaceStr .= '*';
        }
        return substr_replace($account, $replaceStr,0, $filterLen);
    }

    /**
     * 账号过滤
     * @param $account
     * @return mixed
     */
    public static function accountFilter($account)
    {
        $preg_phone = '/^1\d{10}$/ims';
        if(preg_match($preg_phone, $account)) {
            return self::phoneFilter($account);
        } else {
            return self::otherFilter($account);
        }
    }

    /**
     * 获取对象的列
     * @param $objectArray
     * @param $column
     * @return array
     */
    public static function objectColumn($objectArray, $column)
    {
        $arr = [];
        foreach ($objectArray as $object) {
            $arr[] = $object->$column;
        }
        return $arr;
    }

    public static function checkIsDecimal($value)
    {
        $preg= "/^\d+(\.\d{1,2})?$/";
        return preg_match($preg, $value, $arr);
    }

    /**
     * 检查后台权限
     * @return bool
     */
    public function checkBackendPermission($appPermission){
        if(empty($appPermission)){
            return false ;
        }
        if (Yii::$app->admin->can($appPermission) == false && Yii::$app->admin->identity->username!="admin") {
            return false;
        }else{
            return true ;
        }
    }
}
